package org.csu.bbs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.csu.bbs.entity.Posts;
import org.csu.bbs.entity.UserInfo;
import org.csu.bbs.persistence.PostsMapper;
import org.csu.bbs.persistence.SectionMapper;
import org.csu.bbs.persistence.UserInfoMapper;
import org.csu.bbs.result.BetterResult;
import org.csu.bbs.result.Result;
import org.csu.bbs.result.User;
import org.csu.bbs.service.PostsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PostsServiceImpl extends ServiceImpl<PostsMapper,Posts> implements PostsService  {
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    PostsMapper postsMapper;
    @Autowired
    SectionMapper sectionMapper;
    @Override
    public Result AddPosts(Posts posts){
        //SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = new Date(System.currentTimeMillis());
            posts.setDate(date);
            System.out.println(posts);
            postsMapper.insert(posts);
            return new Result(200);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(400);
        }
    }

    public List<Posts> GetBySection(int sectionid){
        if (sectionid == 0){
            return postsMapper.selectList(null);
        }else {
            return postsMapper.selectBySection(sectionid);
        }
    }

    public List<Posts> Search(String keywords) {
        QueryWrapper<Posts> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(keywords!= null, "publisher", keywords).or().like(keywords!=null,"content",keywords);
        return postsMapper.selectList(queryWrapper);
    }

    public Posts GetBySessionAndPassword(String sessionName, String keywords) {
        QueryWrapper<Posts> queryWrapper = new QueryWrapper<>();
        Map<String,Object> map=new HashMap<>();
        map.put("session",sessionName);
        map.put("password",keywords);
        queryWrapper.allEq(map);
        return postsMapper.selectOne(queryWrapper);
    }
    @Override
    public List<Posts> GetByUserId(String userid){
        QueryWrapper<Posts> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("publisher", userid);
        return postsMapper.selectList(queryWrapper);
    }

    @Override
    public List<Posts> GetCollection(String userid) {
        return postsMapper.selectCollection(userid);
    }

    public int DeleteOne(Integer id) {
        return postsMapper.delete(new QueryWrapper<Posts>().eq("id",id));
    }
    @Override
    public ArrayList<Integer> searchCount(){
        QueryWrapper<Posts> b = new QueryWrapper<>();
        QueryWrapper<Posts> c = new QueryWrapper<>();
        QueryWrapper<Posts> d = new QueryWrapper<>();
        QueryWrapper<Posts> e = new QueryWrapper<>();
        QueryWrapper<Posts> f = new QueryWrapper<>();
        b.eq("sectionid",1);
        int count1=postsMapper.selectCount(b);
        c.eq("sectionid",2);
        int count2=postsMapper.selectCount(c);
        d.eq("sectionid",3);
        int count3=postsMapper.selectCount(d);
        e.eq("sectionid",4);
        int count4=postsMapper.selectCount(e);
        f.eq("sectionid",5);
        int count5=postsMapper.selectCount(f);
        ArrayList<Integer> a=new ArrayList<Integer>();
        a.add(0,count1);
        a.add(1,count2);
        a.add(2,count3);
        a.add(3,count4);
        a.add(4,count5);
        System.out.println(a);
        return  a;
    }

    @Override
    public BetterResult<UserInfo> GetPoster(int id) {
        String poster = postsMapper.selectById(id).getPublisher();
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.eq("userid", poster);
        try{
            UserInfo userInfo = userInfoMapper.selectOne(userInfoQueryWrapper);
            return new BetterResult<>(userInfo, 200);
        } catch (Exception e){
            e.printStackTrace();
            return new BetterResult<>(null, 404);
        }
    }

    @Override
    public BetterResult<Integer> LikePost(int id, boolean add) {
        int likes = 0;
        try {
            UpdateWrapper<Posts> postsUpdateWrapper = new UpdateWrapper<>();
            if (add){
                postsUpdateWrapper.eq("id", id)
                                  .setSql("cream = cream + 1");
            }
            else{
                postsUpdateWrapper.eq("id", id)
                                  .setSql("cream = cream - 1");
            }
            postsMapper.update(null, postsUpdateWrapper);
            int state = 200;
            if (add){
                System.out.println("点赞成功！");
            }
            else{
                System.out.println("取消点赞成功！");
                state += 1;
            }
            QueryWrapper<Posts> postsQueryWrapper = new QueryWrapper<>();
            postsQueryWrapper.eq("id", id);
            likes = postsMapper.selectOne(postsQueryWrapper).getCream();
            return new BetterResult<>(likes, state);
        } catch (Exception e){
            e.printStackTrace();
            System.out.println("点赞或取消失败！");
            return new BetterResult<>(likes, 400);
        }
    }
}
